import java.util.ArrayList;
import java.util.Scanner;

public class Main {
	
	public static void displayEvents(ArrayList<Event> events){
		
		for(Event e: events){
			System.out.println(e.toString());
		}
		
	}
	
	public static void liteDisplayEvents(ArrayList<Event> events){
		
		for (Event e: events){
			System.out.print("Event Number: "+(events.indexOf(e)+1));
			System.out.println(" "+e.liteToString());
		}
		
	}
	
	public static Event createNewEvent(Scanner keyboard, String inputLine){
		
		Event e = new Event();
		
		System.out.println("Name of event:");
		inputLine = keyboard.nextLine();
		e.setName(inputLine);
		
		System.out.println("Location of event:");
		inputLine = keyboard.nextLine();
		e.setLocation(inputLine);
		
		int incidentsCounter = 1;
		boolean finished = false;
		
		//create new incidents
		while (!finished){
			Incident i = createNewIncident(keyboard,inputLine,incidentsCounter);
			e.addIncident(i);
			System.out.println("Incident "+incidentsCounter+ " added successfully");
			
			System.out.println("Type y to add another incident, or n to finish.");
			String choice = keyboard.nextLine();
			if (choice.equalsIgnoreCase("n")){
				finished = true;
				System.out.println("Incidents added to "+e.getName());
			}
			incidentsCounter++;
		}	
		
		return e;
	}
	
	public static Incident createNewIncident(Scanner keyboard, String inputLine, int incidentsCounter){
		
		Incident i = new Incident();
		
		i.setNumber(incidentsCounter);
		
		System.out.println("Enter Incident "+incidentsCounter+" Description: ");
		inputLine = keyboard.nextLine();
		i.setDescription(inputLine);
		
		System.out.println("Enter Incident "+incidentsCounter+" Mode: ");
		inputLine = keyboard.nextLine();
		i.setMode(inputLine);
		
		System.out.println("Enter Incident "+incidentsCounter+" Cause: ");
		inputLine = keyboard.nextLine();
		i.setCause(inputLine);
		
		System.out.println("Enter Incident "+incidentsCounter+" Effect: ");
		inputLine = keyboard.nextLine();
		i.setEffect(inputLine);
		
		System.out.println("Enter Incident "+incidentsCounter+" Current Control: ");
		inputLine = keyboard.nextLine();
		i.setCurrentControl(inputLine);
		
		System.out.println("Enter Incident "+incidentsCounter+" Probability of Occurrence: ");
		inputLine = keyboard.nextLine();
		i.setProbOcc(Integer.parseInt(inputLine));
		
		System.out.println("Enter Incident "+incidentsCounter+" Severity: ");
		inputLine = keyboard.nextLine();
		i.setSeverity(Integer.parseInt(inputLine));
		
		System.out.println("Enter Incident "+incidentsCounter+" Probability of Detection: ");
		inputLine = keyboard.nextLine();
		i.setProbDet(Integer.parseInt(inputLine));
		
		i.calculateRPN();
		
		return i;
		
	}
	
	public static void displayEventMenu(){
		
		System.out.println("\nWhat would you like to do?");
		System.out.println();
		System.out.println("type n for new event");
		System.out.println("type v to view event information");
		System.out.println("type p for sorted list of risks according to RPN");
		System.out.println("type r to review initial incident details");
		System.out.println();
		
	}
	
	public static void main(String[] args) {
		
		ArrayList<Event> events = new ArrayList<Event>(0);
		
		Scanner keyboard = new Scanner(System.in);
		String inputLine = "";
		
		boolean finished = false;
		
		while (!(inputLine).equalsIgnoreCase("exit") && (finished == false)){
			
			
			displayEventMenu();
			
			inputLine = keyboard.nextLine();
			
			if (inputLine.equalsIgnoreCase("n")){
				
				//create new event
				Event e = createNewEvent(keyboard,inputLine);
				events.add(e);
				System.out.println("Event added successfully.");
			}
			
			if (inputLine.equalsIgnoreCase("v")){
				
				//load specified event
				if (events.size() == 0){
					System.out.println("No events saved.");
					continue;
				}
				System.out.println("Stored events: ");
				liteDisplayEvents(events);
				System.out.println("\nType the number of the event you would like to view: ");
				System.out.println();
				
				String choice = keyboard.nextLine();
				if (!choice.equalsIgnoreCase("exit")){
					System.out.println(events.get(Integer.parseInt(choice)-1));
				}
				else{
					finished = true;
				}
			}
			
			if (inputLine.equalsIgnoreCase("p")){
				
				//show incidents by priority
				if (events.size() == 0){
					System.out.println("No events saved.");
					continue;
				}
				System.out.println("Stored events: ");
				liteDisplayEvents(events);
				System.out.println("\nType the number of the event you would like to view: ");
				System.out.println("Or type exit to go back to the top menu.");
				System.out.println();
				
				String choice = keyboard.nextLine();
				if (!choice.equalsIgnoreCase("exit")){
					//show sorted list of incidents
					int num = Integer.parseInt(choice);
					Event e = events.get(num-1);
					e.sortIncidents();
					System.out.println(e.toString());
				}
				else{
					finished = true;
				}
				
			}
			
			if (inputLine.equalsIgnoreCase("r")){
				
				//review initial details
				if (events.size() == 0){
					System.out.println("No events saved.");
					continue;
				}
				System.out.println("Stored events: ");
				liteDisplayEvents(events);
				System.out.println("\nType the number of the event you would like to review: ");
				System.out.println("Or type exit to go back to the top menu.");
				System.out.println();
				
				String choice = keyboard.nextLine();
				if (!choice.equalsIgnoreCase("exit")){
					//review selected event's incidents
					ArrayList<Incident> incidents = events.get(Integer.parseInt(choice)-1).getIncidents();
					
					for (Incident i: incidents){
						
						System.out.println("Enter Incident "+i.getNumber()+" Recommended Actions: ");
						inputLine = keyboard.nextLine();
						i.setRecActions(inputLine);
						
						System.out.println("Enter Incident "+i.getNumber()+" Responsibility: ");
						inputLine = keyboard.nextLine();
						i.setResponsibility(inputLine);
						
						System.out.println("Enter Incident "+i.getNumber()+" Target Date: ");
						inputLine = keyboard.nextLine();
						i.setTargetDate(inputLine);
						
						System.out.println("Enter Incident "+i.getNumber()+" Actions Taken: ");
						inputLine = keyboard.nextLine();
						i.setActionTaken(inputLine);
						
						System.out.println("Enter Incident "+i.getNumber()+" New Probability of Occurrence: ");
						inputLine = keyboard.nextLine();
						i.setNewProbOcc(Integer.parseInt(inputLine));
						
						System.out.println("Enter Incident "+i.getNumber()+" New Severity: ");
						inputLine = keyboard.nextLine();
						i.setNewSeverity(Integer.parseInt(inputLine));
						
						System.out.println("Enter Incident "+i.getNumber()+" New Probability of Detection: ");
						inputLine = keyboard.nextLine();
						i.setNewProbDet(Integer.parseInt(inputLine));
						
						i.calculateNewRPN();
						
					}
					
					System.out.println("View reviewed event statistics? type y or n.");
					inputLine = keyboard.nextLine();
					if (!inputLine.equalsIgnoreCase("n")){
						String outputString = events.get(Integer.parseInt(choice)-1).fullToString();
						System.out.println(outputString);
					}
				}
				else{
					finished = true;
				}
				
			}
			
		}
		
		keyboard.close();
		System.out.println("\nApplication terminated");
	}
}